﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Cdhpy.Util
{
    /// <summary>
    /// 读取配置文件中AppSetting项
    /// </summary>
    public sealed class Appsettings
    {
        #region string

        private static string _Get(string value, string defaultVal = "")
        {
            return value ?? defaultVal;
        }

        /// <summary>
        /// 直接通过索引获取appSettings中的某个参数值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Get(string key, string defaultVal = "")
        {
            var value = ConfigurationManager.AppSettings[key];
            return _Get(value, defaultVal);
        }

        /// <summary>
        /// 直接通过索引获取appSettings中的某个参数值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Get(AppSettingsSection setting, string key, string defaultVal = "")
        {
            try
            {
                var value = setting.Settings[key]?.Value;
                return _Get(value, defaultVal);
            }
            catch
            {
                return defaultVal;
            }
        }


        /// <summary>
        /// item与item之间用逗号隔开
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static List<string> _Get_StringList(string value)
        {
            var result = new List<string>();
            if (!String.IsNullOrEmpty(value))
            {
                try
                {
                    result = value.Split(',').ToList();
                }
                catch { }
            }
            return result;
        }

        /// <summary>
        /// item与item之间用逗号隔开
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static HashSet<string> _Get_StringHashset(string value)
        {
            var result = new HashSet<string>();
            if (!String.IsNullOrEmpty(value))
            {
                try
                {
                    var hs = value.Split(',').ToArray();
                    return new HashSet<string>(hs);
                }
                catch { }
            }
            return result;
        }

        /// <summary>
        /// 通过索引获取装载String类型的List集合，各项之间应用英文逗号隔开
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static List<string> Get_StringList(string key)
        {
            var value = ConfigurationManager.AppSettings[key];
            return _Get_StringList(value);
        }

        /// <summary>
        /// 通过索引获取装载String类型的List集合，各项之间应用英文逗号隔开
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static List<string> Get_StringList(AppSettingsSection setting, string key)
        {
            var value = setting.Settings[key]?.Value;
            return _Get_StringList(value);
        }

        /// <summary>
        /// 通过索引获取装载String类型的Hashset集合，各项之间应用英文逗号隔开
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static HashSet<string> Get_StringHashset(string key)
        {
            var value = ConfigurationManager.AppSettings[key];
            return _Get_StringHashset(value);
        }

        /// <summary>
        /// 通过索引获取装载String类型的Hashset集合，各项之间应用英文逗号隔开
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static HashSet<string> Get_StringHashset(AppSettingsSection setting, string key)
        {
            var value = setting.Settings[key]?.Value;
            return _Get_StringHashset(value);
        }


        #endregion

        #region int

        private static int _Get_Int32(string value, int defaultVal = 0)
        {
            int tem;
            if (!int.TryParse(value, out tem))
                tem = defaultVal;
            return tem;
        }

        /// <summary>
        /// 直接通过索引获取某个值类型为Int32的参数值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int Get_Int32(string key, int defaultVal = 0)
        {
            string value = ConfigurationManager.AppSettings[key];
            return _Get_Int32(value, defaultVal);
        }

        /// <summary>
        /// 直接通过索引获取某个值类型为Int32的参数值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int Get_Int32(AppSettingsSection setting, string key, int defaultVal = 0)
        {
            string value = setting.Settings[key]?.Value;
            return _Get_Int32(value, defaultVal);
        }

        #endregion

        #region bool

        /// <summary>
        /// 直接通过索引获取某个值类型为Bool的参数值
        /// </summary>
        /// <param name="value">从settings中读取到的原始值</param>
        /// <param name="defaultToken">特征值</param>
        /// <param name="defaultTokenValue">当匹配特征值时，返回true还是false</param>
        /// <param name="isSensitive">匹配特征值时是否区分大小写</param>
        /// <returns></returns>
        private static bool _Get_Bool(string value, string defaultToken, bool defaultTokenValue, bool isSensitive = false)
        {
            if (value == null) return !defaultTokenValue;
            if (isSensitive)
            {
                if (value == defaultToken) //和true的token匹配
                    return defaultTokenValue;
                else //不匹配
                    return !defaultTokenValue;
            }
            else
            {
                if (value.ToUpper() == defaultToken.ToUpper()) //和true的token匹配
                    return defaultTokenValue;
                else //不匹配返回
                    return !defaultTokenValue;
            }
        }

        /// <summary>
        /// 直接通过索引获取某个值类型为Bool的参数值
        /// </summary>
        /// <param name="value">从settings中读取到的原始值</param>
        /// <param name="defaultToken">特征值</param>
        /// <param name="defaultTokenValue">当匹配特征值时，返回true还是false</param>
        /// <param name="isSensitive">匹配特征值时是否区分大小写</param>
        /// <returns></returns>
        public static bool Get_Bool(string key, string defaultToken, bool defaultTokenValue, bool isSensitive = false)
        {
            string value = ConfigurationManager.AppSettings[key];
            return _Get_Bool(value, defaultToken, defaultTokenValue, isSensitive);
        }

        /// <summary>
        /// 直接通过索引获取某个值类型为Bool的参数值
        /// </summary>
        /// <param name="value">从settings中读取到的原始值</param>
        /// <param name="defaultToken">特征值</param>
        /// <param name="defaultTokenValue">当匹配特征值时，返回true还是false</param>
        /// <param name="isSensitive">匹配特征值时是否区分大小写</param>
        /// <returns></returns>
        public static bool Get_Bool(AppSettingsSection setting, string key, string defaultToken, bool defaultTokenValue, bool isSensitive = false)
        {
            string value = setting.Settings[key]?.Value;
            return _Get_Bool(value, defaultToken, defaultTokenValue, isSensitive);
        }
        #endregion

        #region Regex

        /// <summary>
        /// 直接通过索引获取某个值类型为Regex的参数值
        /// </summary>
        /// <param name="value">正则匹配的Pattern</param>
        /// <param name="ignoreCase">是否忽略大小写</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        private static Regex _Get_Regex(string value, bool ignoreCase = true, Regex defaultValue = null)
        {
            try
            {
                return ignoreCase ? new Regex(value, RegexOptions.IgnoreCase) : new Regex(value);
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 直接通过索引获取某个值类型为Regex的参数值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="ignoreCase">匹配时是否忽略大小写</param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static Regex Get_Regex(string key, bool ignoreCase = true, Regex defaultValue = null)
        {
            string value = ConfigurationManager.AppSettings[key];
            return _Get_Regex(value, ignoreCase, defaultValue);
        }

        /// <summary>
        /// 直接通过索引获取某个值类型为Regex的参数值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="ignoreCase">匹配时是否忽略大小写</param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static Regex Get_Regex(AppSettingsSection setting, string key, bool ignoreCase = true, Regex defaultValue = null)
        {
            string value = setting.Settings[key]?.Value;
            return _Get_Regex(value, ignoreCase, defaultValue);
        }

        #endregion
    }
}
